# -*- coding:utf-8 -*-

# the python module

# the django module
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.template import RequestContext

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.forms import UserChangeForm
from django.contrib.auth.decorators import login_required
from django.http import Http404

from forms import *

from django.utils.translation import ugettext as _

# the SPF module
#from spf.users.models import *

@login_required
def index(request):
    return render_to_response("account/index.html", {"current_title": _("Money account management")}, RequestContext(request))

@login_required
def add_account(request, type):
    mapping = {
        "cash" : [CashAccountForm, _("add cash account")],
        "fixed" : [FixedForm, _("add fixed time bankbook account")],
        "dynamic" : [DynamicForm, _("add dynamic time bankbook account")],
        "credit" : [CreditCardForm, _("add credit card account")],
        "debit" : [DebitCardForm, _("add debit card account")], 
        "insurance" : [InsuranceForm, _("add insurance account")],
        "stock" : [StockForm, _("add stock account")],
        "fund" : [FundForm, _("add fund account")]
    }
    curobj = mapping.get(type, None)
    if curobj is None:
        raise Http404
    title = curobj[1]
    curform = curobj[0]

    if request.method == "POST":
        # handle the post
        form = curform(request.user, request.POST)
        if form.is_valid():
            new_acct = form.save()
            return HttpResponseRedirect(reverse("spf.account.views.display_account", args=[type]))
    else:
        form = curform(request.user)
    return render_to_response("account/add_account.html", {'form' : form, "current_title" : title}, RequestContext(request))

@login_required
def display_account(request, type):
    mapping = {
        "cash" : [CashAccount, _("manage cash account")],
        "fixed" : [FixedTimeBankbookAccount, _("manage fixed time bankbook account")],
        "dynamic" : [DynamicTimeBankbookAccount, _("manage dynamic time bankbook account")],
        "credit" : [CreditcardAccount, _("manage credit card account")],
        "debit" : [DebitcardAccount, _("manage debit card account")],
        "insurance" : [InsuranceAccount, _("manage insurance account")],
        "stock" : [StockAccount, _("manage stock account")],
        "fund" : [FundAccount, _("manage fund account")]
    }
    curobj = mapping.get(type, None)
    if curobj is None:
        raise Http404
    title = curobj[1]
    curobj = curobj[0]
    add_link = reverse("account_add", args=[type])
    try:
        accounts = curobj.objects.filter(owner=request.user).order_by("-id")
        return render_to_response("account/accounts.html", {'accounts' : accounts, "add_link":add_link, "title" : title, "current_title" : _("accounts list")}, RequestContext(request))
    except NameError:
        raise Http404



